package com.work.mdass.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.mdass.domain.Result;
import com.work.mdass.domain.TeachingMaterial;
import com.work.mdass.service.TeachingMaterialService;
import com.work.mdass.util.TeachingMaterialResultUtil;

/*
 * 教材控制器
 */
@Controller
@RequestMapping("/admin/material")
public class TeachingMaterialController {
	
	@Autowired
	private TeachingMaterialService teachingMaterialService;
	
	/*
	 * 添加一本教材
	 */
	@PostMapping("/save")
	@ResponseBody
	public Result<TeachingMaterial> save(@Valid TeachingMaterial teachingMaterial,BindingResult bindingResult){
		
		if(bindingResult.hasErrors()){
			
			return TeachingMaterialResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		
		return TeachingMaterialResultUtil.success(teachingMaterialService.addMaterial(teachingMaterial));
	}
	/*
	 * 获得所有教材
	 */
	@GetMapping("/listTeachingMaterial")
	@ResponseBody
	public Map<Object,Object> getAll(){

		return teachingMaterialService.getALL();
	}
	/*
	 * 更新教材
	 */
	@PostMapping(value ="/updata")
	@ResponseBody
	public Result<TeachingMaterial> updata(@Valid TeachingMaterial teachingMaterial,BindingResult bindingResult){
		if(bindingResult.hasErrors()) {
			return TeachingMaterialResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}
		
		return TeachingMaterialResultUtil.success(teachingMaterialService.updataMaterial(teachingMaterial));
	}
	/*
	 * 删除教材
	 */
	@PostMapping(value = "/delete")
	@ResponseBody
	public Map<Object,Object> delete(@RequestParam(required = false) Integer id){
		
		return teachingMaterialService.deleteMaterial(id);
	}
	/*
	 * 通过关键字查找
	 */
	@GetMapping("/byName")
	@ResponseBody
	public Map<Object,Object> getByName(@RequestParam(required = false) String keyWord){
		
		return teachingMaterialService.getByName(keyWord);
	}
	/*
	 * 通过关键字匹配
	 */
	@PostMapping("/byKeyWord")
	@ResponseBody
	public Map<Object,Object> getByKeyWord(@RequestParam(required = false) String keyWord){
		return teachingMaterialService.getByKeyWord(keyWord);
	}
	
	/*
	 * 个人数据统计
	 */
	@PostMapping("/userAnalysis")
	@ResponseBody
	public Map<Object,Object> forUserAnalysis(@RequestParam(required = false) String tName,
			@RequestParam(required = false) String keyWord){
		return teachingMaterialService.forUserAnalysis(tName, keyWord);
	}
	
	/*
	 * 通过教师名称查找教材
	 */
	@PostMapping("/MaByTName")
	@ResponseBody
	public Map<Object,Object> getByTName(@RequestParam(required = false) String tName){
		
		return teachingMaterialService.getByTName(tName);
	}
	/*
	 * 通过教材编号查找教材
	 */
	@PostMapping("/byCourseSerial")
	@ResponseBody
	public Map<Object,Object> getByCourseSerial(@RequestParam(required =false) String courseSerial){
		return teachingMaterialService.getByCourseSerial(courseSerial);
	}
}
